<?php

/**
 * 项目控制器.
 * User: wangchao
 * Date: 2017/11/18 0018
 * Time: 9:57
 */

namespace User\Controller;

use Common\Controller\CommonController;
use Think\Controller;
use Think\Exception;

class ObjectController extends Controller
{

    /**
     * 获取巡查项目列表
     */
    public function get_object_list()
    {
        $user_id = I('get.user_id');
        $level_id = I('get.level_id');
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $object_ids = M('User')->where(['user_id' => $user_id])->getField('object_ids');
        if (empty($object_ids)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '暂无数据！']);
        }
        $where['object_id'] = ['in', $object_ids];
        $where['status'] = ['elt', 1];
        $object = M('Object')->where($where)->limit($start,C('PAGE_SIZE'))->select();
        // 今天开始的时间戳
        $today = strtotime(date('Y-m-d',time()));
        // 本周开始的时间戳
        $week = strtotime(date('Y-m-d', strtotime("this week Monday", time())));
        // 本旬开始的时间戳
        $d = intval(date('d', time())/10)*10;
        if ($d == 0 ) {
            $d = '01';
        } elseif ($d == 30) {
            $d = '20';
        }
        $date = date('Y-m', time()) . '-' . $d;
        $ten = strtotime($date);
        // 本月开始的时间戳
        $month = mktime(0, 0, 0, date('m'), 1, date('Y'));
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
            $object[$k]['object_level_name'] = M('ObjectLevel')->where(['id' => $v['object_level_id']])->getField('name');
            unset($object[$k]['type_id']);
            unset($object[$k]['danger_id']);
            unset($object[$k]['object_level_id']);
            // 判断今天/本周/本旬/本月是否有过上报记录
            $where['object_id'] = $v['object_id'];
            $where['push_user_id'] =$user_id;
            if ($level_id >= 5) {
                $where['create_time'] = ['gt', $today];
            } elseif ($level_id == 4 || $level_id == 3) {
                $where['create_time'] = ['gt', $week];
            } elseif ($level_id == 2) {
                $where['create_time'] = ['gt', $ten];
            } else {
                $where['create_time'] = ['gt', $month];
            }
            if (M('PushRecord')->where($where)->find()) {
                $object[$k]['is_push'] = 1;
            } else {
                $object[$k]['is_push'] = 0;
            }
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $object]);
    }

    /**
     * 获取巡查项目详情
     */
    public function get_object_desc()
    {
        $object_id = I('get.object_id');
        $info = M('Object')->where(['object_id' => $object_id])->find();
        if (empty($info)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $info['type_name'] = M('ObjectType')->where(['id' => $info['type_id']])->getField('name');
        $info['danger_name'] = M('Danger')->where(['id' => $info['danger_id']])->getField('name');
        $info['object_level_name'] = M('ObjectLevel')->where(['id' => $info['object_level_id']])->getField('name');
        unset($info['type-id']);
        unset($info['danger_id']);
        unset($info['object_level_id']);
        $this->ajaxReturn(['status' => 1, 'msg' => $info]);
    }

    /**
     * 项目上报
     */
    public function post_push_object()
    {
        $post = I('post.');
        $user_id = I('post.user_id');
        $parent_id = M('User')->where(['user_id' => $user_id])->getField('parent_id');
        if (isset($_FILES['img']) && $_FILES['img']['error'] == 0) {
            $ret = upload('img', 'img');
            if ($ret['status'] == 1) {
                $img_url = 'Uploads/' . $ret['image']['origin'];
            } else {
                $img_url = '';
            }
        } else {
            $img_url = '';
        }
        if (isset($_FILES['video']) && $_FILES['video']['error'] == 0) {
            $ret = upload('video', 'video');
            if ($ret['status'] == 1) {
                $video = 'Uploads/' . $ret['image']['origin'];
            } else {
                $video = '';
            }
        } else {
            $video = '';
        }
        M()->startTrans();
        try {
            $data = [
                'object_id' => $post['object_id'],
                'is_danger' => $post['is_danger'],
                'desc' => $post['desc'],
                'deal' => $post['deal'],
                'push_user_id' => $post['user_id'],
                'status' => 1,
                'deal_user_id' => $post['user_id'],
                'pending_user_id' => $parent_id,
                'create_time' => time(),
            ];
            if (isset($post['push_id'])) {
                if ($img_url != '') {
                    $data['img_url'] = $img_url;
                }
                if ($video != '') {
                    $data['video'] = $video;
                }
                $res = M('PushRecord')->where(['push_id' => $post['push_id']])->save($data);
            } else {
                $data['img_url'] = $img_url;
                $data['video'] = $video;
                $res = M('PushRecord')->add($data);
            }
            if (empty($res)) {
                throw new Exception('服务器繁忙，请稍后再试！');
            }
            // 极光推送
            $j_push_id = M('User')->where(['user_id' => $parent_id])->getField('j_push_id');
            if (!empty($j_push_id)) {
                jPushs('你有新的事件待处理！', 1, $j_push_id, $user_id);
            }
            $phone = M('User')->where(['user_id' => $parent_id])->getField('phone');
            if (!empty($phone)) {
                $this->http_send_post($phone);
            }
            M()->commit();
            $this->ajaxReturn(['status' => 1, 'msg' => '提交成功！', 'phone' => $phone]);
        } catch (Exception $e) {
            M()->rollback();
            $this->ajaxReturn(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 获取待处理列表
     */
    public function get_pending_list()
    {
        $user_id = I('get.user_id');
        $level_id = I('get.level_id');
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        /* if ($level_id > 4) {
            $this->ajaxReturn(['status' => 0, 'msg' => '无权访问！']);
        } */
        $where = [
            'pending_user_id' => $user_id,
            'status' => 1,
        ];
        $list = M('PushRecord')->where($where)->limit($start,C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $k => $v) {
            $suggest = M('DealInfo')->where(['push_id' => $v['push_id'], 'deal_user_id' => $v['deal_user_id']])->getField('suggest');
            if (!empty($suggest)) {
                $list[$k]['suggest'] = $suggest;
            } else {
                $list[$k]['suggest'] = '';
            }
            $object = M('Object')->where(['object_id' => $v['object_id']])->find();
            $object['type_name'] = M('ObjectType')->where(['id' => $object['type_id']])->getField('name');
            $object['danger_name'] = M('Danger')->where(['id' => $object['danger_id']])->getField('name');
            $object['object_level_name'] = M('ObjectLevel')->where(['id' => $object['object_level_id']])->getField('name');
            unset($object['type_id']);
            unset($object['danger_id']);
            unset($object['object_level_id']);
            $list[$k]['object'] = $object;
            $list[$k]['deal_user_name'] = M('User')->where(['user_id' => $v['deal_user_id']])->getField('username');
            unset($list[$k]['object_id']);
            unset($list[$k]['deal_user_id']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }

    /**
     * 获取待处理信息详情
     */
    public function get_pending_desc()
    {
        $push_id = I('get.push_id');
        if (empty($push_id)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $info = M('PushRecord')->where(['push_id' => $push_id])->find();
        $object = M('Object')->where(['object_id' => $info['object_id']])->find();
        $object['type_name'] = M('ObjectType')->where(['id' => $object['type_id']])->getField('name');
        $object['danger_name'] = M('Danger')->where(['id' => $object['danger_id']])->getField('name');
        $object['object_level_name'] = M('ObjectLevel')->where(['id' => $object['object_level_id']])->getField('name');
        unset($object['type_id']);
        unset($object['danger_id']);
        unset($object['object_level_id']);
        $info['object'] = $object;
        $info['deal_user_name'] = M('User')->where(['user_id' => $info['deal_user_id']])->getField('username');
        unset($info['object_id']);
        unset($info['deal_user_id']);
        $suggest = M('DealInfo')->where(['push_id' => $push_id, 'deal_user_id' => $info['deal_user_id']])->getField('suggest');
        if (!empty($suggest)) {
            $info['suggest'] = $suggest;
        } else {
            $info['suggest'] = '';
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $info]);
    }

    /**
     * 审核上报事件
     */
    public function post_push_suggest()
    {
        $post = I('post.');
        $user_id = I('post.user_id');
        $level_id = I('post.level_id');
        $push_id = I('post.push_id');
        $suggest = I('post.suggest');
        $is_agree = I('post.is_agree');
        if (empty($push_id) || $push_id == 0) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参错错误！']);
        }
        M()->startTrans();
        try {
            $arr = [
                'push_id' => $push_id,
                'deal_user_id' => $user_id,
                'level_id' => $level_id,
                'suggest' => $suggest,
                'create_time' => date('Y-m-d H:i:s', time()),
                'is_agree' => $is_agree,
            ];
            if (isset($post['id'])) {
                $res = M('DealInfo')->where(['id' => $post['id']])->save($arr);
            } else {
                $res = M('DealInfo')->add($arr);
            }
            if (empty($res)) {
                throw new Exception('添加处理意见失败！');
            }
            //如果同意，status为1，最高领导为2即为归档，新需求车间级也可归档，不同意即is_agree为0则为驳回，修改状态
            if ($is_agree == 1) {
                //1、提交下一步，如果已经是最高级则状态为2归档
                $parent_id = M('User')->where(['user_id' => $user_id])->getField('parent_id');
                $data = [
                    'deal_user_id' => $user_id,
                    'pending_user_id' => $parent_id,
                ];
                // 是否是最高领导审核
                if ($level_id == 1) {
                    $data['status'] = 2;
                } else {
                    $data['status'] = 1;
                }
                M('PushRecord')->where(['push_id' => $push_id])->save($data);
                // 推送
                $j_push_id = M('User')->where(['user_id' => $parent_id])->getField('j_push_id');
                if (!empty($j_push_id)) {
                    jPushs('您有新的事件待处理', 1, $j_push_id, $user_id);
                }
                $phone = M('User')->where(['user_id' => $parent_id])->getField('phone');
                if (!empty($phone)) {
                    $this->http_send_post($phone);
                }
            } elseif ($is_agree == 0) {
                //2、驳回，状态为0，更新下一步处理人，当前处理人
                $deal_user_id = M('PushRecord')->where(['push_id' => $push_id])->getField('deal_user_id');
                $data = [
                    'status' => 0,
                    'deal_user_id' => $user_id,
                    'pending_user_id' => $deal_user_id,
                ];
                M('PushRecord')->where(['push_id' => $push_id])->save($data);
                // 推送
                $j_push_id = M('User')->where(['user_id' => $deal_user_id])->getField('j_push_id');
                if (!empty($j_push_id)) {
                    jPushs('您有事件被驳回！', 2, $j_push_id, $user_id);
                }
                $phone = M('User')->where(['user_id' => $deal_user_id])->getField('phone');
                if (!empty($phone)) {
                    $this->http_send_post($phone);
                }
            } else {
                //归档 默认is_agree=3，更新 状态为2 当前处理人，上级处理人为0；
				$data = [
                    'status' => 2,
                    'deal_user_id' => $user_id,
                    'pending_user_id' => 0,
                ];
                M('PushRecord')->where(['push_id' => $push_id])->save($data);
			}
            M()->commit();
            $this->ajaxReturn(['status' => 1, 'msg' => '提交成功！', 'phone' => $phone]);
        } catch (Exception $e) {
            M()->rollback();
            $this->ajaxReturn(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 获取驳回信息列表
     */
    public function get_reject_list()
    {
        $user_id = I('get.user_id');
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $where = [
            'status' => 0,
            'pending_user_id' => $user_id,
        ];
        $list = M('PushRecord')->where($where)->limit($start,C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $k => $v) {
            $object = M('Object')->where(['object_id' => $v['object_id']])->find();
            $object['type_name'] = M('ObjectType')->where(['id' => $object['type_id']])->getField('name');
            $object['danger_name'] = M('Danger')->where(['id' => $object['danger_id']])->getField('name');
            $object['object_level_name'] = M('ObjectLevel')->where(['id' => $object['object_level_id']])->getField('name');
            unset($object['type_id']);
            unset($object['danger_id']);
            unset($object['object_level_id']);
            $list[$k]['object'] = $object;
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }

    /**
     * 获取驳回信息详情
     */
    public function get_reject_desc()
    {
        $user_id = I('get.user_id');
        $push_id = I('get.push_id');
        if (empty($push_id)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $info = M('PushRecord')->where(['push_id' => $push_id])->find();
        $object = M('Object')->where(['object_id' => $info['object_id']])->find();
        $object['type_name'] = M('ObjectType')->where(['id' => $object['type_id']])->getField('name');
        $object['danger_name'] = M('Danger')->where(['id' => $object['danger_id']])->getField('name');
        $object['object_level_name'] = M('ObjectLevel')->where(['id' => $object['object_level_id']])->getField('name');
        unset($object['type_id']);
        unset($object['danger_id']);
        unset($object['object_level_id']);
        $info['object'] = $object;
        $self_suggest = M('DealInfo')->where(['push_id' => $push_id, 'deal_user_id' => $user_id])->find();
        if (!empty($self_suggest)) {
            $info['self_suggest'] = $self_suggest;
        } else {
            $info['self_suggest'] = [];
        }
        $reject_suggest = M('DealInfo')->where(['push_id' => $push_id, 'deal_user_id' => $info['deal_user_id']])->getField('suggest');
        if (!empty($reject_suggest)) {
            $info['reject_suggest'] = $reject_suggest;
        } else {
            $info['reject_suggest'] = '';
        }
        unset ($info['object_id']);
        $this->ajaxReturn(['status' => 1, 'msg' => $info]);
    }

    /**
     * 查询
     */
    public function get_search_list()
    {
        $user_id = I('get.user_id');
        $level_id = I('get.level_id');
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $get = I('get.');
        /*if ($level_id < 5) {
            $this->ajaxReturn(['status' => 0, '无权访问！']);
        }*/
        $where['push_user_id'] = $user_id;
        if (isset($get['is_danger'])) {
            $where['is_danger'] = $get['is_danger'];
        } else {
            $where['is_danger'] = 1;
        }
        if (isset($get['status'])) {
            $where['status'] = $get['status'];
        } else {
            $where['status'] = 1;
        }
        if (isset($get['object_id'])) {
            $where['object_id'] = $get['object_id'];
        }
        $list = M('PushRecord')->where($where)->limit($start, C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '']);
        }
        foreach ($list as $k => $v) {
            $suggest = M('DealInfo')->where(['push_id' => $v['push_id'], 'deal_user_id' => $v['deal_user_id']])->getField('suggest');
            if (!empty($suggest)) {
                $list[$k]['suggest'] = $suggest;
            } else {
                $list[$k]['suggest'] = '';
            }
            $object = M('Object')->where(['object_id' => $v['object_id']])->find();
            $object['type_name'] = M('ObjectType')->where(['id' => $object['type_id']])->getField('name');
            $object['danger_name'] = M('Danger')->where(['id' => $object['danger_id']])->getField('name');
            $object['object_level_name'] = M('ObjectLevel')->where(['id' => $object['object_level_id']])->getField('name');
            unset($object['type_id']);
            unset($object['danger_id']);
            unset($object['object_level_id']);
            $list[$k]['object'] = $object;
            $list[$k]['deal_user_name'] = M('User')->where(['user_id' => $v['deal_user_id']])->getField('username');
            unset($list[$k]['object_id']);
            unset($list[$k]['deal_user_id']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }

    /**
     * 我的
     */
    public function get_personal_info()
    {
        $user_id = I('get.user_id');
        $level_id = I('get.level_id');
        $data = [];
        // 本月开始的时间戳
        $month = mktime(0, 0, 0, date('m'), 1, date('Y'));
        if ($level_id >= 5) {
            $object_ids = M('User')->where(['user_id' => $user_id])->getField('object_ids');
            $data['object_count'] = count(explode(',', $object_ids));
            $where['create_time'] = ['gt', $month];
            $where['push_user_id'] = $user_id;
            $data['month_deal_count'] = M('PushRecord')->where($where)->count();
            $where['is_danger'] = 1;
            $data['is_danger_count'] = M('PushRecord')->where($where)->count();
            $where['status'] = 2;
            $data['has_deal_count'] = M('PushRecord')->where($where)->count();
            unset($where['status']);
            $data['to_deal_count'] = M('PushRecord')->where($where)->where('status = 1 or status = 0')->count();
        } else {
            $level_user = M('User')->where(['level_id' => $level_id, 'status' => 1])->select();
            $where['create_time'] = ['gt', $month];
            foreach ($level_user as $k => $v) {
                $data[1]['object_count'] += count(explode(',', $v['object_ids']));
                $where['push_user_id'] = $v['user_id'];
                $data[1]['month_deal_count'] += M('PushRecord')->where($where)->count();
                $where['is_danger'] = 1;
                $data[1]['is_danger_count'] += M('PushRecord')->where($where)->count();
                $data[1]['to_deal_count'] += M('PushRecord')->where($where)->where('status=1 or status=0')->count();
            }
            unset($where);
            $all_user = M('User')->where("level_id >= $level_id and status = 1")->select();
            $where['create_time'] = ['gt', $month];
            foreach ($all_user as $k => $v) {
                $data[2]['object_count'] += count(explode(',', $v['object_ids']));
                $where['push_user_id'] = $v['user_id'];
                $data[2]['month_deal_count'] += M('PushRecord')->where($where)->count();
                $where['is_danger'] = 1;
                $data[2]['is_danger_count'] += M('PushRecord')->where($where)->count();
                $data[2]['to_deal_count'] += M('PushRecord')->where($where)->where('status=1 or status=0')->count();
            }
        }
        if (empty($data)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '暂无数据！']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $data]);
    }

    /**
     * 获取系统公告列表
     */
    public function get_msg_list()
    {
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $list = M('Msg')->where(['status' => 1])->limit($start, C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }
    /**
     * 获取系统公告详情
     */
    public function get_msg_detail()
    {
        $msg_id = I('get.msg_id', 1, 'intval');
        $detail = M('Msg')->where(['msg_id' => $msg_id])->find();
        if (empty($detail)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
        $this->ajaxReturn(['status' => 1, 'msg' => $detail]);
    }

    /**
     * 发送短信
     * @param $phone string 手机号
     * @param $msg string 内容
     * @return string
     */
    public function http_send_post($mobile, $msg)
    {
        $url = 'http://115.28.96.167:8081/api/sms/send';   //发送接口
        $post_data['userid']       = '351481';    //用户编号
        $post_data['apikey']       =  'ef9ad3d6bdfe4732812f191ca9f107e0';        //apikey  
        $t = explode(" ", microtime());
        $ts = round(round($t[1].substr($t[0],2,3)));
        $post_data['ts']      = $ts ;   //时间戳，精确到毫秒
        $post_data['sign']  = md5($post_data['userid'].$post_data['ts'].$post_data['apikey']);      //签名
        $post_data['mobile']       = $mobile;                //手机号
        $post_data['msgcontent']      = $msg;                     //发送内容

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
        $result = $this->request_post($url, $post_data);
    }

    function request_post($url = '', $param = '') {
    if (empty($url) || empty($param)) {
        return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}

    /**
     * 风险点获取
     */
    public function get_danger()
    {
        $company_id= I('get.company_id');
        //echo $company_id;exit;
        if (empty($company_id)) {
            $this->ajaxReturn(['status' => 0, 'msg' => 'company_id参数错误！']);
        }
        $dangerM = M('Danger');
        $results = $dangerM->where('company_id='.$company_id)->select();
        if(empty($results)){
            //type_id=113
            $array = array(
                '0'=>array('灭火器是否缺失',$company_id),
                '1'=>array('线路是否老化',$company_id),
                '2'=>array('安全提醒标识是否缺失',$company_id)
            );
            for($i=0;$i<3;$i++){
                 $data['name']=  $array[$i][0];     
                 $data['company_id']=  $array[$i][1];     
                 $data['type_id']=  113;     
                 $data['status']=  1;     
                 $data['is_danger']=  1;
                 $dangerM->add($data);     
            }

        }
        $results = $dangerM->where('company_id='.$company_id)->select();
        //print_r($array);exit;
        $this->ajaxReturn(['status' => 1, 'msg' => $results]);
        exit;
    }
    /**
     * 风险点添加
     */
    public function add_danger()
    {
        $data['name']= I('post.name');
        $data['company_id']= I('post.company_id');
        $data['is_danger']= I('post.is_danger');
        //echo $company_id;exit;
        if (empty($data['name']) || empty($data['company_id']) || empty($data['is_danger'])) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $where['company_id']= $data['company_id'];
        $where['name']= $data['name'];
        $dangerM = M('Danger');
        $findDanger = $dangerM->where($where)->find();
        if(!empty($findDanger)){
            $this->ajaxReturn(['status' => 0, 'msg' => '风险点已存在']);
        }
        $dangerM->add($data);
        
        $results = $dangerM->where('company_id='.$data['company_id'])->select();
        //print_r($array);exit;
        $this->ajaxReturn(['status' => 1, 'msg' => $results]);
        exit;
    }
    /**
     * 风险点添加
     */
    public function add_dangers()
    {
        $data['name']= I('post.name');
        $data['company_id']= I('post.company_id');
        $data['level']= I('post.level');
        $data['hierarchy']= I('post.hierarchy');
        $data['person_charge']= I('post.person_charge');
        //echo $company_id;exit;
        if (empty($data['name'])  || empty($data['company_id']) || empty($data['level']) || empty($data['hierarchy']) || empty($data['person_charge'])) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
        $where['company_id']= $data['company_id'];
        $where['name']= $data['name'];
        $dangerM = M('Dangers');
        $findDanger = $dangerM->where($where)->find();
        if(!empty($findDanger)){
            $this->ajaxReturn(['status' => 0, 'msg' => '风险点已存在']);
        }
        $dangerM->add($data);
        
        //$results = $dangerM->where('company_id='.$data['company_id'])->select();
        //print_r($array);exit;
        $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        exit;
    }
    /**
     * 风险点提交
     */
    public function sub_danger()
    {
        //{{"id":"36","is_danger":"1"},{"id":"37","is_danger":"0"}}
        //$json = '[{"id":"36","is_danger":"1"},{"id":"37","is_danger":"0"}]';
        //$json =json_encode(array(array('id'=>36,'is_danger'=>1),array('id'=>37,'is_danger'=>1)));

        $json = I('post.dangers');
        $array = json_decode($json,true);
        $dangerM = M('Danger');
        foreach ($array as $key => $value) {
            # code...
            $saveData['is_danger'] = $value['is_danger'];
            $result[] = $dangerM->where("id=".$value['id'])->save($saveData);

        }
        $this->ajaxReturn(['status' => 1, 'msg' => $json]);
        exit;
        
    }

     /**
     * 上传测试接口(不需要任何参数)
     * @param  {string} addr:地址，longitude:经度，latitude:纬度
     * @return {json} 成功返回图片路径及参数
    */

    public function upload_images()
    {
        $tmp = file_get_contents("php://input");
        //echo $tmp;
        $arr = json_decode($tmp,true);
        //print_r($arr);
        $picList = $this->_upload();//调用图片上传函数
        if(!$picList){
            $this->error('图片上传失败');
        }
        //print_r($picList);exit;
        $this->ajaxReturn(['status' => 1, 'msg' => $picList]);

    }

    /**
     * 多个图像上传
     * @param {string} $type: 存储类型 report:投诉 ,avatar:头像，logo：厂家logo，ad：广告图片
     * @return {array} $arr 图片web地址列表
     */
    public function _upload($type = 'report'){
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    'Uploads/pc/',
            'savePath'   =>    '',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $baseUrl = C('SITE_URL').'Uploads/pc/';//例如：http://www.baidu.com/up/report/
        $upload = new \Think\Upload($config);// 实例化上传类
        // 上传文件 
        $info = $upload->upload();

        $arr = array();
        $i = 0;
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            foreach($info as $file){ //循环存储图片到服务器
                $subUrl = $file['savepath'].$file['savename'];//例如：20140125/dfuosi203.jpg
                $arr[$i++] = $baseUrl.$subUrl;
            }
            return $arr;
        }

    }
    /**
     * 上传图片接口
     */
    public function upload_image(){
        if (isset($_FILES['img']) && $_FILES['img']['error'] == 0) {
            $ret = upload('img', 'img');
            if ($ret['status'] == 1) {
                $img_url = 'Uploads/' . $ret['image']['origin'];
                $this->ajaxReturn(['status' => 1, 'msg' => $img_url]);
            } else {
                $img_url = '';
                $this->ajaxReturn(['status' => 1, 'msg' => $mg_url]);
            }
        } else{
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 上传图片接口
     */
    public function upload_imagetest(){
        if (isset($_FILES['img']) && $_FILES['img']['error'] == 0) {
            $ret = upload('img', 'img');
            //print_r($ret);exit;
            if ($ret['status'] == 1) {
                $img_url = 'Uploads/' . $ret['image']['origin'];
                $this->ajaxReturn(['status' => 1, 'msg' => $img_url]);
            } else {
                $img_url = '';
                $this->ajaxReturn(['status' => 1, 'msg' => $mg_url]);
            }
        } else{
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 上传视频接口
     */
    public function upload_video(){
        if (isset($_FILES['video']) && $_FILES['video']['error'] == 0) {
            $ret = upload('video', 'video');
            if ($ret['status'] == 1) {
                $video = 'Uploads/' . $ret['image']['origin'];
                $this->ajaxReturn(['status' => 1, 'msg' => $video]);
            } else {
                $video = '';
                $this->ajaxReturn(['status' => 1, 'msg' => $video]);
            }
        } else {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        }
    }
    /**
     * 企业培训接口
     */
    public function enterprise_training(){
        $data['company_id']= I('post.company_id');
        $data['train_name']= I('post.train_name');
        $data['train_time']= I('post.train_time');
        $data['train_content']= I('post.train_content');
        $data['train_photo']= I('post.train_photo');
        $data['create_time']= time();
        if(empty($data['company_id']) || empty($data['train_name']) ){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少参数！']);
        }
        $trainM = M('Train');
        $result = $trainM->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
    /**
     * 获取企业培训列表
     */
    public function get_enterprise_training(){
        $company_id = I('company_id');
        $list = M('train')->where(['company_id'=>$company_id])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count' => $count, 'msg' => $list]);
    }
    /**
     * 提交应急演练接口
     */
    public function add_emergency_drill(){
        $data['company_id'] = I('post.company_id');
        $data['is_plan'] = I('post.is_plan');
        $data['plan_photo'] = I('post.plan_photo');
        $data['drill_time'] = I('post.drill_time');
        $data['drill_address'] = I('post.drill_address');
        $data['drill_item'] = I('post.drill_item');
        $data['is_program'] = I('post.is_program');
        $data['drill_process'] = I('post.drill_process');
        $data['drill_evaluate'] = I('post.drill_evaluate');
        $data['drill_photo'] = I('post.drill_photo');
        $data['create_time'] = time();
        if(empty($data['company_id'])){
            $this->ajaxReturn(['status' => 0,'msg' => '缺少企业id']);
        }
        $result = M('emergency_drill')->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1,'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0,'msg' => '添加失败']);
        }
    }
    /**
     * 企业安全演练接口
     */
    public function enterprise_practice(){
        $data['company_id']= I('post.company_id');
        $data['drill_name']= I('post.drill_name');
        $data['drill_time']= I('post.drill_time');
        $data['drill_content']= I('post.drill_content');
        $data['drill_photo']= I('post.drill_photo');
        $data['create_time']= time();
        if(empty($data['company_id']) || empty($data['drill_name']) ){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少参数！']);
        }
        $trainM = M('Safety_drill');
        $result = $trainM->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
    /**
     * 获取企业安全演练记录列表
     */
    public function get_enterprise_practice(){
        $company_id = I('company_id');
        $list = M('safety_drill')->where(['company_id'=>$company_id])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count'=>$count, 'msg' => $list]);
    }
    /**
     * 获取企业风险排查记录列表
     */
    public function get_trouble_shooting(){
        $company_id = I('company_id');
        $list = M('trouble_shooting')->where(['company_id'=>$company_id])->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }
    /**
     * 添加安全投入记录接口
     */
    public function add_safety_pay(){
        $data['user_id'] = I('user_id');
        $data['company_id'] = I('company_id');
        $company = M('company')->where(['id'=>$data['company_id']])->find();
        $data['company_name'] = $company['name'];
        $data['safety_input_year'] = I('safety_input_year');
        $data['last_year_income'] = I('last_year_income');
        $data['safety_standard'] = I('safety_standard');
        $data['should_take_income'] = I('should_take_income');
        $data['fact_take_income'] = I('fact_take_income');
        $data['totale_safety_pay'] = I('totale_safety_pay');
        $data['safety_pay_photo'] = I('safety_pay_photo');
        $data['create_time'] = time();
        if(empty($data['user_id']) || empty($data['company_id']) ){
            $this->ajaxReturn(['status' => 0, 'msg' => '当前用户编号或企业编号！']);
        }
        $trainM = M('safety_pay');
        $result = $trainM->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
    /**
     * 获取安全投入列表
     */
    public function get_safety_pay_list(){
        $company_id = I('company_id');
        $page = I('get.page',1,'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $list = M('safety_pay')->where(['company_id'=>$company_id])->limit($start,C('PAGE_SIZE'))->select();
        $count = count($list);
        if(empty($list)){
            $this->ajaxReturn(['status'=>0,'msg'=>'没有更多数据！']);
        }
        $front = '"';
        $behind = '"';
        foreach ($list as $key => $value) {
            $json = $front.$value['safety_pay_photo'].$behind;
            $list[$key]['safety_pay_photo'] = json_decode($json,true);
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status'=>1,'count'=>$count,'msg'=>$list]);
    }
    /**
     * 安全投入详情
     */
    public function get_safety_pay_detail(){
        $id = I('get.id', 1, 'intval');
        $detail = M('safety_pay')->where(['id'=>$id])->find();
        if(empty($detail)){
            $this->ajaxReturn(['status'=>0,'msg'=>'没有更多数据！']);
        }
        $front = '"';
        $behind = '"';
        $json = $front.$detail['safety_pay_photo'].$behind;
        $detail['safety_pay_photo'] = json_decode($json,true);
        $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
        $this->ajaxReturn(['status' => 1, 'msg' => $detail]);
    }
    /**
     * 上报审批项目
     */
    public function report_item(){
        $data['uid'] = I('post.uid');
        $data['company_id'] = I('post.company_id');
        $data['report_item'] = I('post.report_item');
        $data['report_content'] = I('post.report_item');
        $data['report_photo'] = I('post.report_photo');
        $data['report_video'] = I('post.report_video');
        $data['create_time'] = time();
        if(empty($data['uid']) || empty($data['report_item'] ) ){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少参数！']);
        }
        $result = M('report')->add($data);
        $apply['item_id'] = $result;
        $apply['handle_type'] = '2';
        $apply['push_user_id'] = $data['uid'];
        $apply['create_time'] = time();
        $res = M('handle_list')->add($apply);
        $user = M('user')->where(['user_id'=>$data['uid']])->find();
        $mobile = $user['phone']; 
        $this->http_send_post($mobile,'【临沭安监】你上报的审批项目已经提交，请等待审批！');
        $user_id = $data['uid'];
        $j_push_id = M('User')->where(['user_id' => $user_id])->getField('j_push_id');
        if (!empty($j_push_id)) {
            jPushs('你上报的审批项目已经提交，请等待审批！', 0, $j_push_id, $user_id);
        }
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
    /**
     * 获取上报审批列表和数量
     */
    public function get_report_item(){
        $uid = I('uid');
        $list = M('report')->where(['uid'=>$uid])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $photo = str_replace('\\', '', $value['report_photo']);
            $photos = str_replace('"', '', $photo);
            $list[$key]['report_photo']= $photos;
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count' => $count, 'msg' => $list]);
    }
    /**
     * 获取企业安全员上报审批的已办和待办
     */
    public function get_report_safety_item(){
        $uid = I('uid');
        $status = I('status');
        $list = M('report')->where(['uid'=>$uid,'status'=>$status])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        // $vfront = '"';
        // $vbehind = '"';
        foreach ($list as $key => $value) {
            // $json = $front.$value['report_photo'].$behind;
            // $vjson = $vfront.$value['report_video'].$vbehind;
            $photo = str_replace('\\', '', $value['report_photo']);
            $photos = str_replace('"', '', $photo);
            $list[$key]['report_photo']= $photos;
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count' => $count, 'msg' => $list]);
    } 
    /**
     * 获取执法人员上报审批的已办和待办
     */
    public function get_report_law_item(){
        $uid = I('uid');
        $status = I('status');
        $list = M('report')->where(['uid'=>$uid,'status'=>$status])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $photo = str_replace('\\', '', $value['report_photo']);
            $photos = str_replace('"', '', $photo);
            $list[$key]['report_photo']= $photos;
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count' => $count, 'msg' => $list]);
    } 
    /**
     * 获取企业安全员申请修改安全员的已办和待办
     */
    public function get_updated_safety_list(){
        $uid = I('uid');
        $status = I('status');
        $list = M('update_safety')->where(['user_id'=>$uid,'status'=>$status])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $front = '"';
        $behind = '"';
        foreach ($list as $key => $value) {
            $json = $front.$value['safety_officer_photo'].$behind;
            $list[$key]['safety_officer_photo'] = json_decode($json,true);
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count' => $count, 'msg' => $list]);
    }
    /**
     * 获取执法人员对申请修改安全员的已办和待办
     */
    public function get_updated_law_list(){
        $uid = I('uid');
        $status = I('status');
        $list = M('update_safety')->where(['user_id'=>$uid,'status'=>$status])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $front = '"';
        $behind = '"';
        foreach ($list as $key => $value) {
            $json = $front.$value['safety_officer_photo'].$behind;
            $list[$key]['safety_officer_photo'] = json_decode($json,true);
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count' => $count, 'msg' => $list]);
    }
    /**
     * 提交安全日志
     */
    public function safety_log(){
        $data['uid'] = I('post.uid');
        $data['company_id'] = I('post.company_id');
        $data['log_title'] = I('post.log_title');
        $data['log_content'] = I('post.log_content');
        $data['log_photo'] = I('post.log_photo');
        $data['log_video'] = I('post.log_video');
        $data['create_time'] = time();
        if(empty($data['uid']) || empty($data['log_title']) ){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少参数！']);
        }
        $result = M('logs')->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }

    }
    /**
     * 获取安全日志列表和数量统计
     */
    public function get_safety_log(){
        $uid = I('uid');
        $list = M('logs')->where(['uid'=>$uid])->select();
        $count = count($list);
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['log_photo']=json_decode($value['log_photo']);
            $list[$key]['log_video']=json_decode($value['log_video']);
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1,'count' => $count, 'msg' => $list]);
    }
    /**
     * 执法人员负责企业列表
     */
    public function company_list(){
        $whereUser['user_id']= I('post.user_id');
        $userM = M("User");   
        $userData = $userM->where($whereUser)->find();
        if($userData['level_id']!=1){
            $this->ajaxReturn(['status' => 0, 'msg' => '请登录执法人员账户']);
        } 
        $array = explode(',', $userData['company_id']);
        $companyM = M("Company"); 
        foreach ($array as $key => $value) {
            # code...
            $whereCompany['id'] =$value;
            $companyData[] = $companyM->where($whereCompany)->find();
            //echo $value;
        }
        $result['user']=$userData;
        $result['company']=$companyData;
        $this->ajaxReturn(['status' => 1, 'msg' => $result]);
        exit;
    }
    
   /**
,     * 企业信息
     */
    public function company_info(){
        $whereCompany['id']= I('post.company_id');
        if(empty($whereCompany['id'])){
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        } 
        $companyM = M("Company");   
        $companyData = $companyM->where($whereCompany)->find();
        if(empty($companyData)){
            $this->ajaxReturn(['status' => 0, 'msg' => '无此用户']);
        } 
        $this->ajaxReturn(['status' => 1, 'msg' => $companyData]);
        exit;
        $array = explode(',', $userData['company_id']);
        $companyM = M("Company"); 
        foreach ($array as $key => $value) {
            # code...
            $whereCompany['id'] =$value;
            $companyData[] = $companyM->where($whereCompany)->find();
            //echo $value;
        }
        $result['user']=$userData;
        $result['company']=$companyData;
        $this->ajaxReturn(['status' => 1, 'msg' => $result]);
        exit;
    }
    /**
     * 包保督导提交
     */
    public function sub_check(){
        $data['company_id'] = I('company_id');
        //$json = '[{"id":"165","is_danger":"1"},{"id":"166","is_danger":"0"},{"id":"167","is_danger":"0"}]';
        $json = I('post.dangers');
        $array = json_decode($json,true);
        $dangerM = M('Danger');
        foreach ($array as $key => $value) {
            # code...
            $saveData['is_danger'] = $value['is_danger'];
            $result[] = $dangerM->where("id=".$value['id'])->save($saveData);

        }
        $data['fire_extinguisher'] = $array[0]['is_danger'];
        $data['line_aging'] = $array[1]['is_danger'];
        $data['safety_sign'] = $array[2]['is_danger'];
        $data['user_id'] = I('user_id');
        $data['content'] = I('content');
        $data['photo'] = I('photo');
        $data['create_time'] = time();
        $result = M('trouble_shooting')->add($data);
        $user = M('user')->where(['company_id'=>$data['company_id']])->where('level_id=5')->find();
        $user_law = M('user')->where(['user_id'=>$data['user_id']])->find();
        $mobile = $user['phone'];
        $person = $user_law['username'];
        $line = $user_law['phone'];
        $content = $data['content'];
        $msg = '【临沭安监】'.'执法人员'.'【'.$person.'/'.$line.'】'.'提交了一条督导内容,'.'【'.$content.'】'.'请注意排查风险';
        $this->http_send_post($mobile,$msg);
        $user_id = $user['user_id'];
        $j_push_id = M('User')->where(['user_id' => $user_id])->getField('j_push_id');
        if (!empty($j_push_id)) {
            jPushs('执法人员'.'【'.$person.'/'.$line.'】'.'提交了一条督导内容,'.'【'.$content.'】'.'请注意排查风险', $j_push_id);
        } 
        $whereCompany['id']= I('post.company_id');
        $dataCompany['is_check_train']= I('post.is_check_train');
        $dataCompany['is_check_drill']= I('post.is_check_drill');
        $dataCompany['is_check_trouble']= I('post.is_check_trouble');
        if(empty($whereCompany['id'])){
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        } 
        $companyM = M("Company");   
        $companyData = $companyM->where($whereCompany)->find();
        if(empty($companyData)){
            $this->ajaxReturn(['status' => 0, 'msg' => '无此企业']);
        } 
        $result = $companyM->where($whereCompany)->save($dataCompany);
        $this->ajaxReturn(['status' => 1, 'msg' => '提交成功']);

        exit;
    }
    
    /**
     * 测试包保督导
     */
    public function sub_checks(){
        $data['company_id'] = I('company_id');
        //$json = '[{"id":"165","is_danger":"1"},{"id":"166","is_danger":"0"},{"id":"167","is_danger":"0"}]';
        $json = I('post.dangers');
        $array = json_decode($json,true);
        $dangerM = M('Danger');
        foreach ($array as $key => $value) {
            # code...
            $saveData['is_danger'] = $value['is_danger'];
            $result[] = $dangerM->where("id=".$value['id'])->save($saveData);

        }
        $data['fire_extinguisher'] = $array[0]['is_danger'];
        $data['line_aging'] = $array[1]['is_danger'];
        $data['safety_sign'] = $array[2]['is_danger'];
        $data['user_id'] = I('user_id');
        $data['content'] = I('content');
        $data['photo'] = I('photo');
        $data['create_time'] = time();
        $result = M('trouble_shooting')->add($data);
        $user = M('user')->where(['company_id'=>$data['company_id']])->where('level_id=5')->find();
        $user_law = M('user')->where(['user_id'=>$data['user_id']])->find();
        $mobile = $user['phone'];
        $person = $user_law['username'];
        $line = $user_law['phone'];
        $content = $data['content'];
        $msg = '【临沭安监】'.'执法人员'.'【'.$person.'/'.$line.'】'.'提交了一条督导内容,'.'【'.$content.'】'.'请注意排查风险';
        $this->http_send_post($mobile,$msg);
        $user_id = $user['user_id'];
        $j_push_id = M('User')->where(['user_id' => $user_id])->getField('j_push_id');
        if (!empty($j_push_id)) {
            jPushs('执法人员'.'【'.$person.'/'.$line.'】'.'提交了一条督导内容,'.'【'.$content.'】'.'请注意排查风险', $j_push_id);
        } 
        $whereCompany['id']= I('post.company_id');
        $dataCompany['is_check_train']= I('post.is_check_train');
        $dataCompany['is_check_drill']= I('post.is_check_drill');
        $dataCompany['is_check_trouble']= I('post.is_check_trouble');
        if(empty($whereCompany['id'])){
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误！']);
        } 
        $companyM = M("Company");   
        $companyData = $companyM->where($whereCompany)->find();
        if(empty($companyData)){
            $this->ajaxReturn(['status' => 0, 'msg' => '无此企业']);
        } 
        $result = $companyM->where($whereCompany)->save($dataCompany);
        $this->ajaxReturn(['status' => 1, 'msg' => '提交成功']);

        exit;
    }
    /**
     * 一张图
     */
    public function map_list(){
        $whereCompany['type']= I('post.type');
        $companyM = M("Company");   
        if(empty($whereCompany['type'])){
            $companyData = $companyM->select();
        } else{
            $companyData = $companyM->where($whereCompany)->select();
        }
        
        
        if(empty($companyData)){
            $this->ajaxReturn(['status' => 0, 'msg' => '无企业']);
        } 
        $this->ajaxReturn(['status' => 1, 'msg' => $companyData]);
        exit;
    }
    /**
     * 企业名称模糊查询
     */
    public function search_company(){
        $name = I('name'); 
        $where['name'] = array('like','%'.$name.'%');
        $company = M('company');
        if(empty($name)){
            $list = $company->select();
        }else{
            $list = $company->where($where)->select();
        }
        if(empty($list)){
            $this->ajaxReturn(['status' => 0, 'msg' => '无企业']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);

    }
    /**
     * 应急物资列表
     */
    public function goods_list(){
        //$whereCompany['type']= I('post.type');
        /*if(empty($whereCompany['type'])){
            $this->ajaxReturn(['status' => 1, 'msg' => '参数错误！']);
        } */
        $rescueM = M("Emergency_rescue");   
        $rescueData = $rescueM->select();
        if(empty($rescueData)){
            $this->ajaxReturn(['status' => 0, 'msg' => '无物资']);
        } 
        $this->ajaxReturn(['status' => 1, 'msg' => $rescueData]);
        exit;
    }
    
    /**
     * 提交企业电量
     */
    public function electricity_ranking(){
        $data['uid'] = I('post.uid');
        $data['company_id'] = I('post.company_id');
        $company = M('company')->where(['id'=>$data['company_id']])->find();
        $data['company_name'] = $company['name']; 
        $data['elec_number'] = I('post.elec_number');
        $data['elec_photo'] = I('post.elec_photo');
        $data['remark'] = I('post.remark');
        $data['create_time'] = time();
        
        if(empty($data['uid'] || empty($data['company_id']))){
            $this->ajaxReturn(['status'=>0,'msg'=>'参数错误！']);
            exit;
        }
        
        if(!empty($data)){
            $result = M('electricity_ranking')->add($data);
            if($result){
                $this->ajaxReturn(['status'=>1,'msg'=>$data]);
            }else{
                $this->ajaxReturn(['status'=>0,'msg'=>'添加失败']);
            }
        }
        
        

    }
    /**
     * 企业电量排行列表
     */
    public function get_elec_list(){
        $page = I('get.page',1,'intval');
        $start = ($page-1)*C('PAGE_SIZE');

        $list = M('electricity_ranking')->limit($start,C('PAGE_SIZE'))->order('elec_number desc')->select();
        if(empty($list)){
            $this->ajaxReturn(['status'=>0,'msg'=>'没有更多数据！']);
        }
        $this->ajaxReturn(['status'=>1,'msg'=>$list]);
        
    }
    
    /**
     * 安全员获取上传电量列表和数量
     */
    public function get_elec_safety_list(){
        $uid = I('uid');
        $page = I('get.page',1,'intval');
        $start = ($page-1)*C('PAGE_SIZE');

        $list = M('electricity_ranking')->limit($start,C('PAGE_SIZE'))->where(['uid'=>$uid])->order('elec_number desc')->select();
        $count = count($list);
        if(empty($list)){
            $this->ajaxReturn(['status'=>0,'msg'=>'没有更多数据！']);
        }
        $front = '"';
        $behind = '"';
        
        foreach ($list as $key => $value) {
            $json = $front.$value['elec_photo'].$behind;
            $list[$key]['elec_photo']= json_decode($json,true);
            $list[$key]['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status'=>1,'count'=> $count,'msg'=>$list]);
    }
    /**
     * 安全员电量详情
     */
    public function get_elec_detail(){
        $msg_id = I('get.id', 1, 'intval');
        $detail = M('electricity_ranking')->where(['id' => $msg_id])->find();
        if (empty($detail)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $front = '"';
        $behind = '"';
        $json = $front.$detail['elec_photo'].$behind;
        $detail['elec_photo'] = json_decode($json,true);
        $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
        $this->ajaxReturn(['status' => 1, 'msg' => $detail]);
    }
    /**
     * 企业安全排行
     */
    public function get_safety_ranking(){
        $page = I('get.page',1,'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $list = M('safety_ranking')->field('any_value(company_id) as company_id,any_value(company_name) as company_name,any_value(sum(score)) as score')->group('company_id')->limit($start,C('PAGE_SIZE'))->order('score desc')->select();
        if(empty($list)){
            $this->ajaxReturn(['status'=>0,'msg'=>'没有更多数据！']);
        }
        $this->ajaxReturn(['status'=>1,'msg'=>$list]);
    }
    /**
     * 获取森林防火巡检分类
     */
    public function get_fire_item(){
        $item = M('fire_item')->select();
        if(empty($item)){
            $this->ajaxReturn(['status'=>0,'msg'=>'没有更多数据！']);
        }else{
            $this->ajaxReturn(['status'=>1,'msg'=>$item]);
        }

    }
    /**
     * 添加森林防火巡检分类
     */
    public function add_fire_item(){
        $data['user_id'] = I('post.user_id');
        $data['name'] = I('post.name');
        if(empty($data)){
            $this->ajaxReturn(['status'=>0,'msg'=>'不能为空']);
        }
        if(!empty($data)){
            $result = M('fire_item')->add($data);
            if($result){
                $this->ajaxReturn(['status'=>1,'msg'=>$data]);
            }else{
                $this->ajaxReturn(['status'=>0,'msg'=>'添加失败']);
            }
        }
    }
    /**
     * 提交森林防火巡检记录
     */
    public function fire_prevention(){
        if(IS_POST){
            $data['uid'] = I('post.uid');
            $data['fire_item'] = I('post.fire_item');
            $item_name = M('fire_item')->where(['id'=>$data['fire_item']])->find();
            $data['item_name'] = $item_name['name'];
            $data['fire_content'] = I('post.fire_content');
            $data['fire_photo'] = I('post.fire_photo');
            $data['fire_gps_position'] = I('fire_gps_position');
            $position = explode(',', $data['fire_gps_position']);
            $data['lng'] = $position[0];
            $data['lat'] = $position[1];
            $data['create_time'] = time();
            if(empty($data['fire_item'] || empty($data['fire_content']))){
                $this->ajaxReturn(['status'=>0,'msg'=>'参数错误！']);
            }
            if(!empty($data)){
                $result = M('fire_prevention')->add($data);
                if($result){
                    $this->ajaxReturn(['status'=>1,'msg'=>$data]);
                }else{
                    $this->ajaxReturn(['status'=>0,'msg'=>'添加失败']);
                }
            }
        }
        
    }
    /**
     * 获取森林防火巡检记录接口
     */
    public function get_fire_prevention(){
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $list = M('fire_prevention')->limit($start, C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }
    /**
     * 山林资料列表
     */
    public function get_forest_list(){
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $list = M('forest_data')->limit($start, C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }
    /**
     * 获取山林资料详情
     */
    public function get_forest_detail(){
        $id = I('get.id', 1, 'intval');
        $detail = M('forest_data')->where(['forest_id' => $id])->find();
        if (empty($detail)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
        $this->ajaxReturn(['status' => 1, 'msg' => $detail]);
    }
    /**
     * 增加环保巡检项目分类
     */
    public function add_environment_item(){
        $data['user_id'] = I('post.user_id');
        $data['item_name'] = I('post.item_name');
        if(empty($data)){
            $this->ajaxReturn(['status'=>0,'msg'=>'不能为空']);
        }
        if(!empty($data)){
            $result = M('environment_item')->add($data);
            if($result){
                $this->ajaxReturn(['status'=>1,'msg'=>$data]);
            }else{
                $this->ajaxReturn(['status'=>0,'msg'=>'添加失败']);
            }
        }
    }
    /**
     * 获取环保巡检项目分类
     */
    public function get_environment_item(){
        $item = M('environment_item')->select();
        if(empty($item)){
            $this->ajaxReturn(['status'=>0,'msg'=>'没有更多数据！']);
        }else{
            $this->ajaxReturn(['status'=>1,'msg'=>$item]);
        }
    }
    /**
     * 提交环保巡检记录
     */
    public function environment_protect(){
        if(IS_POST){
            $data['uid'] = I('post.uid');
            $data['env_item'] = I('post.env_item');
            $item_name = M('environment_item')->where(['item_id'=>$data['env_item']])->find();
            $data['item_name'] = $item_name['item_name'];
            $data['env_content'] = I('post.env_content');
            $data['env_photo'] = I('post.env_photo');
            $data['env_gps_position'] = I('env_gps_position');
            $position = explode(',', $data['env_gps_position']);
            $data['lng'] = $position[0];
            $data['lat'] = $position[1];
            $data['create_time'] = time();
            if(empty($data['env_item'] || empty($data['env_content']))){
                $this->ajaxReturn(['status'=>0,'msg'=>'参数错误！']);
            }
            if(!empty($data)){
                $result = M('environment_protect')->add($data);
                if($result){
                    $this->ajaxReturn(['status'=>1,'msg'=>$data]);
                }else{
                    $this->ajaxReturn(['status'=>0,'msg'=>'添加失败']);
                }
            }
        }
    }
    /**
     * 获取环保巡检记录列表
     */
    public function get_environment_protect(){
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $list = M('environment_protect')->limit($start, C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }
    /**
     * 获取环保资料列表
     */
    public function get_env_data_list(){
        $page = I('get.page', 1, 'intval');
        $start = ($page-1)*C('PAGE_SIZE');
        $list = M('environment_data')->limit($start, C('PAGE_SIZE'))->select();
        if (empty($list)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        foreach ($list as $key => $value) {
            $list[$key]['datetime']=date('Y-m-d H:i:s',$value['create_time']);
        }
        $this->ajaxReturn(['status' => 1, 'msg' => $list]);
    }
    /**
     * 获取环保资料详情
     */
    public function get_env_data_detail(){
        $id = I('pro.id', 1, 'intval');
        $detail = M('environment_data')->where(['forest_id' => $id])->find();
        if (empty($detail)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
        $this->ajaxReturn(['status' => 1, 'msg' => $detail]);
    }
    /**
     * 获取环保巡检记录详情
     */
    public function get_environment_protect_detail(){
        $id = $_GET['id'];
        $detail = M('environment_protect')->where(['id' => $id])->find();
        if (empty($detail)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
        $this->ajaxReturn(['status' => 1, 'msg' => $detail]);
    } 
    /**
     * 获取防火巡检记录详情
     */
    public function get_fire_prevention_detail(){
        $id = $_GET['id'];
        $detail = M('fire_prevention')->where(['id' => $id])->find();
        if (empty($detail)) {
            $this->ajaxReturn(['status' => 0, 'msg' => '没有更多数据！']);
        }
        $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
        $this->ajaxReturn(['status' => 1, 'msg' => $detail]);
    }

    public function aa_test(){
        $aa = '"Uploads\/pc\/20200628\/5ef84e689194a.jpg","Uploads\/pc\/20200628\/5ef84e68934aa.jpg","Uploads\/pc\/20200628\/5ef84e6894a03.jpg"';
        $bb =  str_replace('\\', '', $aa);
        $cc = str_replace('"', '', $bb);
        echo $cc;
        exit;
        // $user_id = '74';
        // $j_push_id = M('User')->where(['user_id' => $user_id])->getField('j_push_id');
        // if (!empty($j_push_id)) {
        //     jPushs('你有新的事件待处理！', 1, $j_push_id, $user_id);
        // }
    }
    /**
     * 隐患排查治理
     */
    public function hidden_danger(){
        if(IS_POST){
            $data['company_id'] = I('post.company_id');
            $data['uid'] = I('post.uid');
            $data['select_time'] = I('post.select_time');
            $data['select_level'] = I('post.select_level');
            $data['select_person'] = I('post.select_person');
            $data['select_description'] = I('post.select_description');
            $data['select_img'] = I('post.select_img');
            $data['parts'] = I('post.parts');
            $data['danger_level'] = I('post.danger_level');
            $data['measure'] = I('post.measure');
            $data['person_charge'] = I('post.person_charge');
            $data['end_date'] = I('post.end_date');
            $data['accept_date'] = I('post.accept_date');
            $data['accept'] = I('post.accept');
            $data['accept_img'] = I('post.accept_img');

            $result = M('hidden_danger')->add($data);
            if($result){
                $this->ajaxReturn(['status'=>1,'msg'=>$data]);
            }else{
                $this->ajaxReturn(['status'=>0,'msg'=>'提交失败！']);
            }
            exit;
            
        }
    }
    /**
     * 企业培训接口2
     */
    public function enterprise_training2(){
        $data['company_id']= I('post.company_id');
        $data['train_name']= I('post.train_name');
        $data['train_time']= I('post.train_time');
        $data['train_content']= I('post.train_content');
        $data['train_photo']= I('post.train_photo');
        $data['is_plan']= I('post.is_plan');
        $data['plan_img']= I('post.plan_img');
        $data['create_time']= time();
        if(empty($data['company_id']) || empty($data['train_name']) || empty($data['is_plan'])){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少参数！']);
        }
        $trainM = M('Train');
        $result = $trainM->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
    /**
     * 新员工三级安全教育
     */
    public function safety_education(){
        $data['company_id']= I('post.company_id');
        $data['name']= I('post.name');
        $data['sex']= I('post.sex');
        $data['position']= I('post.position');
        $data['factory_time']= I('post.factory_time');
        $data['workshop']= I('post.workshop');
        $data['card_id']= I('post.card_id');
        $data['phone']= I('post.phone');
        $data['is_special']= I('post.is_special');
        $data['kind']= I('post.kind');
        $data['license_date']= I('post.license_date');
        $data['review_date']= I('post.review_date');
        $data['company_level']= I('post.company_level');
        $data['workshop_level']= I('post.workshop_level');
        $data['team_level']= I('post.team_level');
        $data['train_img']= I('post.train_img');
        $data['create_time']= date("Y-m-d H:i:s",time());
        if(empty($data['company_id']) || empty($data['name']) || empty($data['position'])){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少参数！']);
        }
        $trainM = M('safety_education');
        $result = $trainM->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
    /**
     * 年度再培训
     */
    public function retraining(){
        $data['company_id']= I('post.company_id');
        $data['address']= I('post.address');
        $data['train_time']= I('post.train_time');
        $data['teacher']= I('post.teacher');
        $data['train_number']= I('post.train_number');
        $data['qualified']= I('post.qualified');
        $data['disqualified']= I('post.disqualified');
        $data['evaluate']= I('post.evaluate');
        $data['train_img']= I('post.train_img');
        $data['create_time']= time();
        if(empty($data['company_id']) || empty($data['address']) || empty($data['train_time'])){
            $this->ajaxReturn(['status' => 0, 'msg' => '缺少参数！']);
        }
        $trainM = M('retrain');
        $result = $trainM->add($data);
        if($result){
            $this->ajaxReturn(['status' => 1, 'msg' => $data]);
        }else{
            $this->ajaxReturn(['status' => 0, 'msg' => '添加失败']);
        }
    }
}
